/* 
 * File:   KeyboardParameterCommand.cpp
 * Author: Gab'z
 * 
 * Created on 5 juin 2012, 12:29
 */

#include "KeyboardParameterCommand.h"
#include "../../Context.h"
#include " ../../../../memento/BooleanParameterMemento.h"

namespace calculator {

    KeyboardParameterCommand::KeyboardParameterCommand(const std::string name) : ParametersCommand(name) {
    }

    KeyboardParameterCommand::KeyboardParameterCommand(const KeyboardParameterCommand& orig) : ParametersCommand(orig._name) {
    }

    KeyboardParameterCommand::~KeyboardParameterCommand() {
    }

    KeyboardParameterCommand* KeyboardParameterCommand::clone() const {
        return new KeyboardParameterCommand(_name);
    }

    void KeyboardParameterCommand::apply(Parameters* parameters) const {
        parameters->setDisplayKeyboard(!parameters->getDisplayKeyboard());
    }

    const Memento* KeyboardParameterCommand::createMemento() const throw (CommandException) {
        return new BooleanParameterMemento(clone(), Context::getInstance()->getParameters()->getDisplayKeyboard());
    }

    void KeyboardParameterCommand::restoreFromMemento(const Memento* memento) const throw (MementoException) {
        const BooleanParameterMemento* booleanParameterMemento = dynamic_cast<const BooleanParameterMemento*> (memento);
        if (booleanParameterMemento == 0) {
            std::string str = "Bad Memento type, expected : BooleanParameterMemento, was ";
            str += typeid (*memento).name();
            throw MementoException(str);
        }
        Context::getInstance()->getParameters()->setDisplayKeyboard(booleanParameterMemento->_boolean);
    }
}